<template>
  <div class="fund-list-page">
    <!-- 顶部区域：背景+导航+搜索 -->
    <div class="top-section">
      <div class="top-content">
        <!-- 标题栏 -->
        <div class="header-bar">
          <div class="header-left">
            <div class="logo-container">
              <img src="../assets/logo.png" alt="Logo" class="logo" />
            </div>
            <h1 class="main-title">基金成绩单（股票型基金版）</h1>
          </div>
        </div>

        <!-- 搜索栏 -->
        <div class="search-container">
          <div class="search-wrapper">
            <div class="search-dropdown">
              <span>基金代码</span>
              <span class="dropdown-icon">▼</span>
            </div>
            <div class="search-input-group">
              <input v-model="fundCode" type="text" class="fund-code-input" placeholder="001595" />
              <button class="search-btn" @click="searchFunds">
                <span class="search-icon">
                  <img src="../assets/搜索.png" alt="搜索" />
                </span>
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 基金列表区域（白底） -->
    <div class="list-section">
      <div class="fund-list-container">
        <div class="fund-cards">
          <div v-for="(fund, index) in funds" :key="index" class="fund-card" @click="viewFundDetail(fund)">
            <div class="fund-name">{{ fund.name }} <span class="fund-code">({{ fund.code }})</span></div>
            <div class="fund-info">
              <div class="info-left">
                <div class="info-item">
                  <div class="info-label">规模：</div>
                  <div class="info-value">{{ fund.size }}</div>
                </div>
                <div class="info-item">
                  <div class="info-label">基金经理：</div>
                  <div class="info-value">{{ fund.manager }}</div>
                </div>
                <div class="info-item">
                  <div class="info-label">管理人：</div>
                  <div class="info-value">{{ fund.company }}</div>
                </div>
              </div>
              <div class="info-right">
                <div class="growth-label">成立以来涨幅</div>
                <div class="growth-value">{{ fund.growth }}</div>
                <div class="fund-date">({{ fund.establishDate }})</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 客服图标 -->
    <div class="customer-service" @click="showCustomerServiceDialog">
      <div class="service-icon-container">
        <div class="service-icon-border">
          <img src="../assets/小鸡客服.svg" alt="客服" />
        </div>
        <div class="service-label">客服基基</div>
      </div>
    </div>

    <!-- 客服弹窗 -->
    <div class="service-dialog" v-if="isServiceDialogVisible" @click.self="hideCustomerServiceDialog">
      <div class="service-dialog-content">
        <div class="service-dialog-header">
          <div class="service-dialog-title">
            <img src="../assets/小鸡客服.svg" alt="客服" class="service-dialog-icon" />
            <span>智能基基</span>
          </div>
          <div class="service-dialog-close" @click="hideCustomerServiceDialog">×</div>
        </div>
        <div class="service-dialog-body">
          <div class="chat-messages">
            <div class="chat-message chat-message-ai">
              <p>你好，我是智能客服，有什么可以帮你～</p>
            </div>
            <div v-if="selectedQuestion" class="chat-message chat-message-user">
              <p>{{ selectedQuestion }}</p>
            </div>
            <div v-if="selectedQuestion" class="chat-message chat-message-ai">
              <p v-if="selectedQuestion === '股票型基金是什么？'">
                股票型基金是以投资为主要股票类别的（通常80%以上资产配置股票）的基金产品。
              </p>
              <p v-else>我正在查询相关信息，请稍等...</p>
            </div>
          </div>
        </div>
        <div class="service-quick-options">
          <div class="quick-option" 
               :class="{ 'active': selectedQuestion === '股票型基金是什么？' }" 
               @click="selectOption('股票型基金是什么？')">
            股票型基金是什么？
          </div>
          <div class="quick-option" 
               :class="{ 'active': selectedQuestion === '什么能力看出这个水平' }" 
               @click="selectOption('什么能力看出这个水平')">
            什么能力看出这个水平
          </div>
          <div class="quick-option" 
               :class="{ 'active': selectedQuestion === '收益大多靠基管平？' }" 
               @click="selectOption('收益大多靠基管平？')">
            收益大多靠基管平？
          </div>
          <div class="quick-option" 
               :class="{ 'active': selectedQuestion === '股票型基金到底还是股票' }" 
               @click="selectOption('股票型基金到底还是股票')">
            股票型基金到底还是股票
          </div>
        </div>
        <div class="service-dialog-footer">
          <input type="text" placeholder="输入你的问题" v-model="userQuestion" @keyup.enter="sendQuestion" />
          <button @click="sendQuestion">发送</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const fundCode = ref('')
const funds = ref([])
const isServiceDialogVisible = ref(false)
const userQuestion = ref('')
const selectedQuestion = ref('')

// 显示客服弹窗
const showCustomerServiceDialog = () => {
  isServiceDialogVisible.value = true
}

// 隐藏客服弹窗
const hideCustomerServiceDialog = () => {
  isServiceDialogVisible.value = false
}

// 选择快速问题选项
const selectOption = (option) => {
  selectedQuestion.value = option
  userQuestion.value = option
}

// 发送问题
const sendQuestion = () => {
  if (userQuestion.value.trim()) {
    selectedQuestion.value = userQuestion.value
    userQuestion.value = ''
  }
}

// 搜索基金
const searchFunds = () => {
  fetchFunds()
}

// 获取基金列表
const fetchFunds = async () => {
  try {
    // 模拟API请求
    generateMockData()
  } catch (error) {
    console.error('获取基金列表失败:', error)
    generateMockData()
  }
}

// 生成模拟数据
const generateMockData = () => {
  funds.value = Array(9).fill().map(() => ({
    name: '平安先锋主题混合发起A',
    code: '019457',
    size: '4.14亿元',
    growth: '450%',
    manager: '张俊先',
    company: '平安基金',
    establishDate: '2025-05-09'
  }))
}

// 查看基金详情
const viewFundDetail = (fund) => {
  router.push({ name: 'fund-score-card', params: { fundCode: fund.code } })
}

onMounted(() => {
  fetchFunds()
})
</script>

<style scoped>
.fund-list-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

/* 顶部区域样式 */
.top-section {
  background: url('../assets/背景图.png');
  background-size: cover;
  background-position: center;
  position: relative;
  color: white;
  padding-bottom: 30px;
}

.top-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 16, 36, 0.8);
  z-index: 0;
}

.top-content {
  position: relative;
  z-index: 1;
  padding: 20px 0;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.header-bar {
  display: flex;
  justify-content: flex-start;
  padding: 0 20px;
  margin-bottom: 30px;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-container {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #61a8ff;
  margin-right: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo {
  width: 80%;
  height: 80%;
  object-fit: contain;
}

.main-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: white;
}

.search-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 0 20px;
}

.search-wrapper {
  display: flex;
  background-color: white;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.search-dropdown {
  background-color: white;
  color: #333;
  padding: 0 15px;
  display: flex;
  align-items: center;
  white-space: nowrap;
  border-right: 1px solid #e0e0e0;
}

.dropdown-icon {
  font-size: 12px;
  margin-left: 5px;
  color: #999;
}

.search-input-group {
  display: flex;
  flex: 1;
}

.fund-code-input {
  flex: 1;
  padding: 10px 15px;
  background: white;
  border: none;
  color: #333;
  font-size: 16px;
}

.fund-code-input::placeholder {
  color: #aaa;
}

.search-btn {
  background-color: #61a8ff;
  border: none;
  color: white;
  padding: 0 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-icon img {
  width: 20px;
  height: 20px;
}

/* 列表区域样式 */
.list-section {
  flex: 1;
  background-color: white;
  border-top: 1px solid #e0e0e0;
  padding: 20px 0;
}

.fund-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.fund-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.fund-card {
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: white;
}

.fund-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  border-color: #d0d0d0;
}

.fund-name {
  font-size: 15px;
  font-weight: 500;
  color: #333;
  margin-bottom: 15px;
}

.fund-code {
  font-weight: normal;
  color: #666;
}

.fund-info {
  display: flex;
}

.info-left {
  flex: 1;
}

.info-item {
  display: flex;
  margin-bottom: 8px;
}

.info-label {
  color: #999;
  width: 75px;
}

.info-value {
  color: #333;
}

.info-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.growth-label {
  font-size: 13px;
  color: #666;
  margin-bottom: 5px;
}

.growth-value {
  font-size: 20px;
  font-weight: 600;
  color: #ff4d4f;
}

.fund-date {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.customer-service {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 100;
  cursor: pointer;
}

.service-icon-container {
  width: 70px;
  height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.service-icon-border {
  width: 60px;
  height: 60px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
  overflow: hidden;
}

.customer-service img {
  width: 52px;
  height: 52px;
  object-fit: contain;
}

.service-label {
  font-size: 12px;
  color: #333;
  margin-top: 5px;
  white-space: nowrap;
  text-align: center;
}

/* 客服弹窗样式 */
.service-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  z-index: 101;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: flex-end;
}

@keyframes slideInRight {
  from { opacity: 0; transform: translateX(100%); }
  to { opacity: 1; transform: translateX(0); }
}

.service-dialog-content {
  background-color: white;
  box-shadow: -2px 0 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 380px;
  animation: slideInRight 0.3s;
}

.service-dialog-header {
  background-color: #87ceeb;
  color: white;
  padding: 12px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.service-dialog-title {
  display: flex;
  align-items: center;
}

.service-dialog-icon {
  width: 24px;
  height: 24px;
  margin-right: 10px;
}

.service-dialog-close {
  cursor: pointer;
  font-size: 20px;
  font-weight: bold;
}

.service-dialog-body {
  padding: 15px;
  flex: 1;
  overflow-y: auto;
}

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.chat-message {
  max-width: 80%;
  padding: 10px 15px;
  border-radius: 15px;
}

.chat-message-ai {
  background-color: #f0f9ff;
  align-self: flex-start;
  border-top-left-radius: 0;
}

.chat-message-user {
  background-color: #e1f3d8;
  align-self: flex-end;
  border-top-right-radius: 0;
}

.service-quick-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0 15px 10px;
}

.quick-option {
  background-color: #f5f5f5;
  padding: 8px 12px;
  border-radius: 15px;
  font-size: 13px;
  cursor: pointer;
  color: #666;
  border: 1px solid #e8e8e8;
  transition: all 0.2s;
}

.quick-option:hover {
  background-color: #e8f5ff;
  color: #4b97d8;
  border-color: #cce7ff;
}

.quick-option.active {
  background-color: #e8f5ff;
  color: #4b97d8;
  border-color: #cce7ff;
}

.service-dialog-footer {
  display: flex;
  border-top: 1px solid #eee;
  padding: 10px 15px;
}

.service-dialog-footer input {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 8px 10px;
  margin-right: 10px;
}

.service-dialog-footer button {
  background-color: #87ceeb;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0 15px;
  cursor: pointer;
}

@media (max-width: 1200px) {
  .fund-cards {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 992px) {
  .fund-cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .search-wrapper {
    flex-direction: column;
  }
  
  .search-dropdown {
    padding: 10px 15px;
    border-right: none;
    border-bottom: 1px solid #e0e0e0;
  }
  
  .fund-cards {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .service-dialog-content {
    width: 300px;
  }
}

@media (max-width: 576px) {
  .fund-cards {
    grid-template-columns: 1fr;
  }
  
  .service-dialog-content {
    width: 100%;
    max-width: 100%;
  }
}
</style> 